package com.gullmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gullmall.product.dao.AttrAttrgroupRelationDao;
import com.gullmall.product.dao.CategoryDao;
import com.gullmall.product.entity.AttrAttrgroupRelationEntity;
import com.gullmall.product.entity.AttrEntity;
import com.gullmall.product.service.AttrService;
import com.gullmall.product.service.CategoryService;
import com.gullmall.product.vo.AttrGroupRelationVo;
import com.gullmall.product.vo.AttrVo;
import com.gullmall.product.vo.GroupAndAttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gullmall.common.utils.PageUtils;
import com.gullmall.common.utils.Query;

import com.gullmall.product.dao.AttrGroupDao;
import com.gullmall.product.entity.AttrGroupEntity;
import com.gullmall.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrGroupService")
@Transactional
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId) {
        IPage<AttrGroupEntity> page;
        String key = (String) params.get("key");//查询关键字,如果有的话
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.like("attr_group_name", key).or().like("descript", key);
            });
        }
        if (catId != 0) {
            wrapper.eq("catelog_id", catId);
        }
        //select * from pms_attr_group where catelog_id =catid and (attr_group_name like key  or descript like key )
        page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);

    }


    @Override
    public Long[] findCatalogPath(Long catalogId) {
        Long[] catalogPath = categoryService.findCatalogPath(catalogId);
        return catalogPath;
    }

    @Override
    public void removeAttrGroupAndRelation(List<Long> attrGroupIds) {
        //移除attr_group表
        this.removeBatchByIds(attrGroupIds);

        attrAttrgroupRelationDao.updateBatchByAttrGroupId(attrGroupIds);
    }

    @Override
    public void updateBatch(List<AttrGroupRelationVo> attrGroupRelationVoList) {
        List<AttrAttrgroupRelationEntity> relationEntityList = attrGroupRelationVoList.stream().map(vo -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(vo, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());

        if (relationEntityList!=null && relationEntityList.size()>0){
            UpdateWrapper<AttrAttrgroupRelationEntity> updateWrapper;

            for (AttrAttrgroupRelationEntity relation : relationEntityList) {
                updateWrapper=new UpdateWrapper<>();
                updateWrapper.eq("attr_id",relation.getAttrId());
                attrAttrgroupRelationDao.update(relation, updateWrapper);
            }
        }
    }

    @Override
    public List<GroupAndAttrVo> getAttrGroupAndAttrsByCatalogId(Long catalogId) {

        //获取该类别下的所有分组
        List<AttrGroupEntity> attrGroupEntity =
                this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catalogId));

        //获取每个分组下的所有属性
        List<GroupAndAttrVo> collect = attrGroupEntity.stream().map(group -> {
            GroupAndAttrVo groupAndAttrVo = new GroupAndAttrVo();
            BeanUtils.copyProperties(group, groupAndAttrVo);
            List<AttrEntity> attr = attrService.getRelationAttr(groupAndAttrVo.getAttrGroupId());
            if (attr != null && attr.size() > 0) {
                groupAndAttrVo.setAttrs(attr);
            }
            return groupAndAttrVo;
        }).collect(Collectors.toList());
        return collect;
    }


}